package top.zt9.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.search.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.zt9.service.ElasticsearchService;

import java.io.IOException;
import java.util.Map;

/**
 * @Author ztf
 * @Date 17:30 2021/11/27
 **/
@RestController
@RequestMapping(value = "/search")
public class SearchController {
    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private ElasticsearchService elasticsearchService;

    @PostMapping(value = "searchCount")
    public long searchCount(@RequestParam("params") String params) throws IOException {
        //将params反序列化为map对象
        Map map = objectMapper.readValue(params, Map.class);
        //调用service查询总条数
        Long count =  elasticsearchService.logsCount(map);
        //返回
        return count;
    }
}
